<?php

namespace plugin\webman\houwang;
use plugin\webman\common\Common;
use GatewayWorker\Lib\Gateway;
use Workerman\Timer;
// use support\Request;
use support\Redis;
use support\Db;
use plugin\webman\houwang\Houwang;


class Event_houwang
{

    public static function onWorkerStart($worker)
    {

        if($worker->id === 0){  //只有进程id是0的才执行该定时器
            $key = 'timer_houwang';
            $timer = Redis::get($key);
            echo ("炸猴王定时器".$timer);
            if($timer==0){
                Timer::add(1, function(){
                    Houwang::timer();   //定时发送各个时间阶段的通知
                });
                Redis::set($key, 1); // echo('定时器启动');
            }
            
        }
    }
        

    public static function onConnect($client_id)
    {
        // // 向当前client_id发送数据
        // Gateway::sendToAll("$client_id 刚刚上线");
    }

    public static function onWebSocketConnect($client_id, $data)
    { 
        Gateway::sendToAll("$client_id 上线了");
    }

    public static function onMessage($client_id,$message)
    {   
        var_dump($message);
        $data=is_array($message) ? $message : json_decode($message,1);

        
        $action= $data['action'] ? $data['action'] :'other';

        switch ($action) {
            case 'checkurl':
                $checkurl = Common::checkurl($client_id,$data);
                Gateway::sendToClient($client_id,Common::returndata('returncheckurl',$checkurl));
                break;
            case 'getconfig':
                $config = Common::getconfig2();
                Gateway::sendToClient($client_id,Common::returndata('returnconfig',$config));
                break;
            //注册 
            case 'userreg':
                $reg = Common::userreg($data['data']);
                
                Gateway::sendToClient($client_id,Common::returndata('returnuserreg',$reg));
                break;
            // 登录
            case 'login':
                $res = Common::userlogin($data['data']);
                if($res['code']==1){
                    $key = 'binduid_'.$client_id;  //绑定uid写入redis
                    $useredis = Redis::set($key,$res['info']['userinfo']['id']);
                    Gateway::bindUid($client_id,$res['info']['userinfo']['id']);
                }
                Gateway::sendToClient($client_id,Common::returndata('returnlogin',$res));
                break;
            case 'enterroom':
                $res = Houwang::enterroom($client_id,$data['data']);
                Gateway::sendToClient($client_id,Common::returndata('returnenterroom',$res));
                break;
            case 'touzhu':
                $res = Houwang::touzhu($client_id,$data['data']);
                Gateway::sendToClient($client_id,Common::returndata('returntouzhu',$res));
                break;
            case 'robotbet':
                $res = Houwang::robotbet($client_id,$data['data']);
                Gateway::sendToClient($client_id,Common::returndata('returnrobotbet',$res));
                break;
            case 'paihang':
                $res = Houwang::paihang($client_id,$data['data']);
                Gateway::sendToClient($client_id,Common::returndata('returnpaihang',$res));
                break;
            case 'jiangchi':
                $res = Houwang::jiangchi($client_id,$data['data']);
                Gateway::sendToClient($client_id,Common::returndata('returnjiangchi',$res));
                break;
        }

        // Gateway::sendToClient($client_id, "未定义事件名");
    }

    
    public static function onClose($client_id)
    {

    }


}
